package cn.liuyi.weiyi.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
import cn.edu.pku.tools.BitmapHelper;
import cn.edu.pku.tools.RegistrationInfo;
import cn.edu.pku.tools.TransactionRecord;
import cn.edu.pku.tools.YJRApplication;
import cn.liuyi.weiyi.AddDoctorActivity;
import cn.liuyi.weiyi.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author vector
 *
 */
public class AddDoctorAdapter extends BaseAdapter{

	Context context;
	LayoutInflater inflater;
	ImageLoader imageLoader;
	DisplayImageOptions options;
	//Typeface typeFace;
	public AddDoctorAdapter(Context context,ImageLoader imageLoader, DisplayImageOptions options) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		this.imageLoader = imageLoader;
		this.options = options;
		//typeFace =Typeface.createFromAsset(context.getAssets(),"fonts/hei.ttf");
	}

	@Override
	public int getCount() {
		
		//return doctors.length();
		return AddDoctorActivity.doctors.length();
	}

	@Override
	public Object getItem(int position) {
		try {
			return AddDoctorActivity.doctors.getJSONObject(position);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		JSONObject doctor = null;
		String name =null;
		String hospital = null;
		String head = null;
		String category = null;
		String section = null;
		String title = null;
		String title_name = null;
		boolean recommend = false;
		try {
			doctor = AddDoctorActivity.doctors.getJSONObject(position);
			name = doctor.getString("name");
			hospital = doctor.getJSONObject("hospital").getString("name");
			head = doctor.getString("avatar");
			category = doctor.getString("category");
			recommend = doctor.getBoolean("recommend");
			section = doctor.getJSONObject("section").getString("text");
			title = doctor.getString("title");
			//title_name = YJRApplication.initData.getJSONObject("codings").getJSONObject("doctoryCategory").getJSONObject(category).getJSONObject("title").getString(title);
			title_name = YJRApplication.initData.getJSONObject("response").getJSONObject("codings").getJSONObject("doctorCategory").getJSONObject(category).getJSONObject("title").getString(title);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ViewHolder holder;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.add_doctor_item, parent,false);
			holder.recommend_imv = (ImageView) convertView.findViewById(R.id.recommend);
			holder.doctor_name_tv = (TextView) convertView.findViewById(R.id.doctor_name_tv);
			holder.work_info_tv = (TextView) convertView.findViewById(R.id.work_info_tv);
			holder.zc_info_tv= (TextView) convertView.findViewById(R.id.zc_info_tv);
			holder.zc_name_tv= (TextView) convertView.findViewById(R.id.zc_name_tv);
			holder.doctor_head_imv = (ImageView) convertView.findViewById(R.id.doctor_head_imv);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		if(recommend==true){
			holder.recommend_imv.setVisibility(View.VISIBLE);
		}else{
			holder.recommend_imv.setVisibility(View.INVISIBLE);
		}
		holder.doctor_name_tv.setText(strSwitch(name));
		holder.work_info_tv.setText(strSwitch(hospital) +" "+ strSwitch(section) +" " + strSwitch(title_name));
		holder.zc_name_tv.setText("众筹项目名称");
		holder.zc_info_tv.setText("￥10,100已筹/52赞");
		//holder.zc_info_tv.setTypeface(typeFace);
		//holder.doctor_head_imv.setImageBitmap(BitmapHelper.toRoundBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.head)));
		imageLoader.displayImage(YJRApplication.head_path+head, holder.doctor_head_imv, options);
		return convertView;
		
	}

    class ViewHolder {
    	
        RelativeLayout doctor_info_rl,crowd_program_rl,chat_rl;
        ImageView doctor_head_imv, recommend_imv;
        TextView doctor_name_tv,work_info_tv,zc_info_tv,zc_name_tv;
    }
    
    private String strSwitch(String str){
    	if(str==null||str.equals("null"))
    		return "";
    	return str;
    }
	  
}
